#!/usr/bin/env node

import * as path from 'path';
import { generate } from './index';
import { logError } from './utils';

function resolve(...dir: string[]) {
    return path.resolve(process.cwd(), ...dir);
}

function run() {
    const args = process.argv.slice(2);
    if (!args[0]) {
        logError('extract-ts-declaration: Please type you input file');
        return;
    }
    const outIdx = args.indexOf('-o');
    let output = resolve('dist', 'typings');
    if (outIdx >= 0 && outIdx < args.length - 1) {
        output = resolve(args[outIdx + 1]);
    }

    const fileNameIdx = args.indexOf('-f');
    let fileName = 'main.d.ts';
    if (fileNameIdx >= 0 && fileNameIdx < args.length - 1) {
        fileName = args[fileNameIdx + 1];
    }

    try {
        generate({
            input: resolve(args[0]),
            output,
            autoClear: args.includes('-c'),
            fileName,
        });
    } catch (error: any) {
        logError(error.message);
    }
}

run();